<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../vue.js"></script>
</head>

<body>
    <!-- 在vue渲染的过程中 diff算法是一直在工作的,但是diff算法的比较是默认按照顺序依次比较的
    如果说我们逆序添加数据,则很可能diff算法的对比错位了
    所以我们在列表渲染的时候要添加一个唯一的key,diff算法的比较就变成了key对应的新旧虚拟DOM的比较 -->
    <div id="app">
        <button @click="addPersonBefore">在前面添加一个人</button>
        <button @click="addPersonAfter">在后面添加一个人</button>
        <ul>
            <!-- key前面要加：不然就只是普通的属性 -->
            <li v-for="item in persons" :key="item.id">
                姓名：{{item.name}}--年龄{{item.age}}
            </li>
        </ul>
    </div>

    <script>
        Vue.config.prodactionTip = false

        const vm = new Vue({
            el: "#app",
            data() {
                return {
                    persons: [{
                            id: "001",
                            name: "laowang",
                            age: 19
                        },
                        {
                            id: "002",
                            name: "xiaowang",
                            age: 29
                        },
                        {
                            id: "003",
                            name: "zhongwang",
                            age: 9
                        },
                    ],
                };

            },
            methods: {
                addPersonBefore() {
                    this.persons = [{
                            id: Date.now(),
                            name: "dawang",
                            age: 22
                        },
                        ...this.persons,
                    ];
                },
                addPersonAfter() {
                    this.persons = [
                        ...this.persons,
                        {
                            id: Date.now(),
                            name: "wangzhongwang",
                            age: 32
                        },
                    ];
                },
            },

        })
    </script>
</body>

</html>